home *** CD-ROM | disk | FTP | other *** search
- { ERF.PLB #2.00 85-08-06 GAUSSIAN ERROR-FUNCTION APPROXIMATION
-
- V02 L00 85-08-06 conversion to Turbo Pascal by Dennis E. Hamilton,
- for separate testing for use as part of the Chi-Squared
- Distribution procedure, Q2(x, df).
-
- V01 L01 79-03-22 implementated in Benton Harbor BASIC 05.01.01 by DEH;
- used in testing that BASIC's awful random-number generator.
- L00 79-03-18 Texas Instruments SR52 calculator program derived
- by Dennis E. Hamilton to verify basic recurrence method by
- checking against values in standard tables. }
-
-
- function
-
- erf(z: real {limit of integration} )
-
- :real {value of the Gaussian Error Function at z};
-
-
- {The Gaussian error function is the integral, from 0 to z, of
-
- 2/sqrt(pi)*exp(-sqr(t)) dt
-
- with negative z also defined by erf(-z) = -erf(z).
-
- This function can be used as the basis for computation of the related
- Gaussian probability functions, including the Normal Probability
- distribution and the chi-Squared distribution functions. }
-
-
- const
-
- e1max = 1E-10;
- {largest positive value of x for which exp(x) is exactly 1.0
- to the floating-point precision of this computer. This value
- is used to avoid computation of insignificant terms.}
-
- e0min = 81;
- {smallest nonzero value of x at which exp(-x) underflows to
- 0.0 within the computer's floating-point precision. This value
- is used to steer around avoidable arithmetic overflows.}
-
-
- function H1(x: real)
-
- :real {approximation of 1-erf(abs(x)) where erf(x) is
- the standard Gaussian error integral [HMF: 7.1.1]};
-
- const a6 = 0.0000430638; a5 = 0.0002765672;
- a4 = 0.0001520143; a3 = 0.0092705272;
- a2 = 0.0422820123; a1 = 0.0705230784;
-
- xmax = 9 {value of sqrt(e0min)};
-
- begin {Computation to over 6 decimal-place precision based on Hastings
- approximation 7.1.28 in [Abramowitz, Milton., Stegun, Irene A.
- (eds.) HANDBOOK OF MATHEMATICAL FUNCTIONS. National Bureau of
- Standards Applied Mathematics Series Publication #55. Dover
- Publications (New York: 1965)]. The 4-place version given
- there [HMF: 7.1.26] is also perfectly usable, and is to be
- recommended when e1max is larger than 1E-6 or so. The present
- form has been chosen to avoid reliance on exp(x), so that
- availability and reliability of any built-in exp(x) function
- does not have to be presumed in order to obtain erf(x) and its
- relatives, such as the normal probability function P(x). }
-
- x := abs(x);
- if x > xmax
- then H1 := 0
- else begin
- x := (((((a6*x + a5)*x + a4)*x + a3)*x + a2)*x + a1)*x + 1.0;
- H1 := sqr(sqr(sqr(sqr(1.0/x))));
- end;
- end {H1};
-
-
- BEGIN {erf};
-
- if z < 0.0
- then erf := H1(z) - 1.0
- else erf := 1.0 - H1(z);
-
- {Note that the loss of precision invariably accompanying addition
- and subtraction of 1.0 when H1(z) is small can often be forestalled by
- using H1 directly in places where erf(z) is required and the sign of
- z is already known. }
-
- END {erf};
-
- { The following results were obtained with a test program (ERFT1.PAS)
- compiled and executed using Borland International Turbo Pascal 3.00A for
- CP/M-80. This version maintains a 40-bit floating-point significand and
- decimal output conversion rounded to 11 significant digits. The error
- terms represent the absolute difference found by consulting known
- tabulations provided along with the z values in file ERFT1.DAT:
-
-
- ERFT1> #2.00 85-08-06 STANDARD ERROR FUNCTION TEST & DEMONSTRATION
-
- z erf(z) abs(error)
-
- 0.000000000E+00 0.0000000000E+00 0.00E+00
- 2.579182479E-11 0.0000000000E+00 2.58E-11
- 2.579282480E-11 2.9103830457E-11 3.31E-12
- 1.000000000E-10 8.7311491370E-11 1.27E-11
- 0.000001000 0.00000112836 1.28E-07
-
- 0.010000000 0.01128332760 8.80E-08
- 0.020000000 0.02256441954 1.55E-07
- 0.030000000 0.03384101848 2.04E-07
- 0.050000000 0.05637172353 2.54E-07
- 0.060000000 0.06762133443 2.60E-07
- 0.080000000 0.09007788447 2.41E-07
- 0.090000000 0.10128037352 2.20E-07
-
- The values computed for erf(z), with z small, have the
- indicated variance from table values of The Handbook
- of Mathematical Functions [HMF: Table 7.1]. Results
- to within 1E-5 are usually quite acceptable.
-
- z erf(z) abs(error)
-
- 0.180000000 0.20093593110 9.21E-08
- 0.370000000 0.39920611672 1.33E-07
-
- 0.460000000 0.48465529432 9.57E-08
- 0.470000000 0.49374493215 1.19E-07
- 0.480000000 0.50274953019 1.41E-07
- 0.490000000 0.51166810037 1.61E-07
- 0.500000000 0.52049969828 1.80E-07
-
- 0.600000000 0.60385583273 2.58E-07
- 0.810000000 0.74800336344 8.28E-08
-
- Midrange values, near .5, reveal the best that can be
- obtained when either erf(z) or erfc(z) = 1-erf(c) is
- used. Test values are from [HMF: Table 7.1].
-
- z erf(z) abs(error)
-
- 1.830000000 0.99034701019 2.05E-07
- 2.506628275 0.99960703858 2.11E-07
- 3.069980124 0.99998570912 1.46E-07
- 3.544907702 0.99999943889 2.59E-08
- 3.963327298 0.99999997623 2.97E-09
- 4.341607527 0.99999999890 2.95E-10
-
- 4.689472100 0.99999999994 5.46E-11
- 5.000000000 1.00000000000 1.82E-12
- 1.000000000E+01 1.00000000000 0.00E+00
-
- Near the tail, as erf(z) approaches 1.0, [HMF: Table 7.3]
- values of erfc(z) = 1 - erf(z) are used to see how well
- accuracy is maintained despite adjustments by 1.0 in erf.
-
- ERFT1> End of test.
- }
- (* end of ERF.PLB *)